/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * SdMilePostTemplateController 里程碑模板
 * <p>
 * 修改纪录 2020-12-25 版本：1.0 lanjianwei 创建。
 *
 * @version 版本：1.0
 * @author 作者：lanjianwei 创建日期：2020-12-25
 */

package com.topscomm.gpm.controller;

import com.alibaba.fastjson.JSONArray;
import com.topscomm.basic.exception.MessageException;
import com.topscomm.gpm.controller.auto.GpmMilePostTemplateControllerAuto;
import com.topscomm.gpm.pojo.GpmMilePostTemplateEntity;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.tap.ITapService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: lanjianwei
 * @date: 2020-12-25
 * @description:里程碑模板Controller
 * @modify:
 */
@RestController
@RequestMapping("sd/milePostTemplate")
public class GpmMilePostTemplateController extends GpmMilePostTemplateControllerAuto implements
        IGpmMilePostTemplateController {

    /**
     * @author: lanjianwei
     * @date: 2020-12-25
     * @param paraMap
     * @return
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    protected String spellListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_milepost")) {
            String value = paraMap.get("qry_milepost").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and name like '%{0}%' ", value);
            }
        }

        if (paraMap.containsKey("qry_code")) {
            String value = paraMap.get("qry_code").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and code = '{0}' ", value);
            }
        }

        if (paraMap.containsKey("qry_projectlevel")) {
            String value = paraMap.get("qry_projectlevel").toString();
            if (!StringUtil.isEmpty(value)) {
                String[] split = value.split(",");
                if (split.length > 1) {
                    for (int i = 0; i < split.length; i++) {
                        if (i == 0) {
                            sql.appendSingle("and (projectlevel like '%{0}%'", split[i]);
                        } else if (i == split.length - 1) {
                            sql.appendSingle(" or projectlevel like '%{0}%')", split[i]);
                        } else {
                            sql.appendSingle(" or projectlevel like '%{0}%'", split[i]);
                        }
                    }
                } else {
                    sql.appendSingle("and projectlevel like '%{0}%'", split[0]);
                }
            }
        }
        if (paraMap.containsKey("qry_projecttype")) {
            String value = paraMap.get("qry_projecttype").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and  projecttype in ({0})", value);
            }
        }
        return sql.toString();
    }

    /**
     * @author: lanjianwei
     * @date: 2020-12-25
     * @param paraMap
     * @return
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    @GetMapping(value = "/refList")
    public ResponseResult<Object> refList(
        @RequestParam(required = false) Map<String, Object> paraMap) {
        return super.refList(paraMap);
    }


    @Override
    @PostMapping(value = "/insert")
    public ResponseResult<Object> insert(@RequestBody(required = false) Map<String, Object> paraMap) {
        try{
            this.beforeInsert(paraMap, paraMap);
            //创建审批人资源池对象
            GpmMilePostTemplateEntity entity = new GpmMilePostTemplateEntity();
            entity.setName(ConvertUtil.convertToString(paraMap.get("name")));
            entity.setProjecttype(ConvertUtil.convertToString(paraMap.get("projecttype")));
            String str = paraMap.get("sortcode").toString();
            for(int i=0;i<str.length();i++){
                if(!Character.isDigit(str.charAt(i))){
                    throw new MessageException("输入的序号必须为数字！");
                }
            }
            entity.setSortCode(Integer.parseInt(paraMap.get("sortcode").toString()));
//            entity.setProjectlevel(ConvertUtil.convertToString(paraMap.get("projectlevel")));
            // 对项目级别做处理
            JSONArray levelArray = JSONArray.parseArray(paraMap.get("projectlevel").toString());
            StringBuffer sb = new StringBuffer();
            for(int i=0;i<levelArray.size();i++){
                String level = levelArray.get(i).toString();
                if(i==levelArray.size()-1){
                    sb.append(level);
                }else{
                    sb.append(level + ",");
                }
            }
            entity.setProjectlevel(ConvertUtil.convertToString(sb));
            entity.setDeliverable(ConvertUtil.convertToString(paraMap.get("deliverable")));
            entity.setTemplatetype(ConvertUtil.convertToString(paraMap.get("templatetype")));
            this.service.insert(entity);
            Map<String, Object> dataMap = entity.convertToMap();
            return ResponseResult.ok(dataMap);
        }catch (Exception var5){
            return this.handleControllerException(var5);
        }
    }


    @Override
    @GetMapping(value = "/list")
    public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            paraMap = BaseUtil.decodeSecureMap(paraMap);
            Query query = new Query(paraMap);
            String sqlWhere = this.spellListSql(paraMap);
            int totalCount = this.service.getCount(sqlWhere);
            List<Map<String, Object>> tempList = null;
            Map<String, Object> paraMap2 = new HashMap<>();
            paraMap2.put("curPageIndex", query.getCurrentPage());
            int beginCount = query.getCurrentPage() * query.getPageSize() - query.getPageSize();
            paraMap2.put("begincount", beginCount);
            paraMap2.put("pagesize", query.getPageSize());
            paraMap2.put("sidx", query.getSidx());
            paraMap2.put("sord", query.getSord());
            paraMap2.put("whereSql", sqlWhere);
            if (query.getPageSize() > 0) {
                tempList = this.service.queryMapForPage(paraMap2,"queryMapForPageExtends");
            } else {
                tempList = this.service.queryMap(paraMap2,"queryMapByWhereExtends");
            }
            this.service.setDispFields(tempList);
            Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
            return ResponseResult.ok(page);
        } catch (Exception var7) {
            return this.handleControllerException(var7);
        }
    }
}

